package com.example.huichaoliu;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.huichaoliu.adapters.OrderAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.source.utils.Utils;

public class OrderActivity extends BaseActivity implements OnClickListener {

	private ListView listView = null;
	private TextView tv_to_all = null;
	private TextView tv_to_pay = null;
	private TextView tv_to_get = null;
	private TextView tv_to_send = null;
	private JSONArray ja = new JSONArray();
	private JSONArray jaToPay = new JSONArray();
	private JSONArray jaToSend = new JSONArray();
	private JSONArray jaAfterSend = new JSONArray();
	private JSONArray jaToComment = new JSONArray();
	private JSONArray jaFinished = new JSONArray();
	private JSONArray jaBacking = new JSONArray();
	private JSONArray jaBacked = new JSONArray();
	
	public static final int CODE_ALL = -1;
	public static final int CODE_TO_PAY = 0;
	public static final int CODE_TO_SEND = 1;
	public static final int CODE_SENDED = 2;
	public static final int CODE_TO_COMMENT = 3;
	public static final int CODE_FINISHED = 4;
	public static final int CODE_BACKING = 5;
	public static final int CODE_BACKED = 6;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_order);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		case R.id.tv_to_all:
			tv_to_all.setBackgroundColor(0xff1E1E1E);
			tv_to_get.setBackgroundColor(0xff000000);
			tv_to_pay.setBackgroundColor(0xff000000);
			tv_to_send.setBackgroundColor(0xff000000);
			break;
		case R.id.tv_to_get:
			tv_to_all.setBackgroundColor(0xff000000);
			tv_to_get.setBackgroundColor(0xff1E1E1E);
			tv_to_pay.setBackgroundColor(0xff000000);
			tv_to_send.setBackgroundColor(0xff000000);
			break;
		case R.id.tv_to_pay:
			tv_to_all.setBackgroundColor(0xff000000);
			tv_to_get.setBackgroundColor(0xff000000);
			tv_to_pay.setBackgroundColor(0xff1E1E1E);
			tv_to_send.setBackgroundColor(0xff000000);
			break;
		case R.id.tv_to_send:
			tv_to_all.setBackgroundColor(0xff000000);
			tv_to_get.setBackgroundColor(0xff000000);
			tv_to_pay.setBackgroundColor(0xff000000);
			tv_to_send.setBackgroundColor(0xff1E1E1E);
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("我的订单");
		findViewById(R.id.left_button).setOnClickListener(this);
		
		listView = (ListView) findViewById(R.id.listView);
		tv_to_all = (TextView) findViewById(R.id.tv_to_all);
		tv_to_get = (TextView) findViewById(R.id.tv_to_get);
		tv_to_pay = (TextView) findViewById(R.id.tv_to_pay);
		tv_to_send = (TextView) findViewById(R.id.tv_to_send);
		tv_to_all.setOnClickListener(this);
		tv_to_get.setOnClickListener(this);
		tv_to_pay.setOnClickListener(this);
		tv_to_send.setOnClickListener(this);
	}

	@Override
	public void initData() {
		try{
			JSONObject jo = new JSONObject();
			jo.put("userId", Config.getUserId(mContext));
			jo.put("pageno", 1);
			jo.put("pagesize", 100);
			jo.put("state", -1);
			Bundle bundle = new Bundle();
			bundle.putString("request", Utils.requestParamsEncode(jo));
			Log.e("jo", jo.toString());
			HTTPRequest.POST(this, Config.REQUEST_URLS.URL_ORDER_LIST, bundle, new onRequestOverListener() {
				@Override
				public void onRequestOver(String result) {
					try{
						JSONObject jo = new JSONObject(result);
						if(jo.getInt("code") != 0){
							showToast(jo.getString("message"));
							return;
						}
						ja = jo.getJSONArray("response");
						for(int i = 0; i < ja.length();i++){
							JSONObject joNew = ja.getJSONObject(i);
							switch(joNew.getInt("state")){
							case CODE_TO_PAY:
								jaToPay.put(joNew);
								break;
							case CODE_TO_SEND:
								jaToSend.put(joNew);
								break;
							case CODE_SENDED:
								jaAfterSend.put(joNew);
								break;
							case CODE_TO_COMMENT:
								jaToComment.put(joNew);
								break;
							case CODE_FINISHED:
								jaFinished.put(joNew);
								break;
							case CODE_BACKING:
								jaBacking.put(joNew);
								break;
							case CODE_BACKED:
								jaBacked.put(joNew);
								break;
							}
						}
						listView.setAdapter(new OrderAdapter(mContext,ja));
					}catch(JSONException e){
						e.printStackTrace();
					}
				}
			});
		}catch(Exception e){
			
		}
	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

}
